//
//  MyUIDatePicker.m
//  cocoapodText
//
//  Created by apple on 16/8/20.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShiPei.h"
#import "MyUIDatePicker.h"
#import <Foundation/Foundation.h>

@interface MyUIDatePicker()
@property(nonatomic,strong)NSDate *date;
@end

@implementation MyUIDatePicker

/**
 *  初始化视图属性
 *
 *  @param frame 视图frame
 *
 *  @return self
 */
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //定制View
        self.backgroundColor = [UIColor clearColor];
        self.userInteractionEnabled = YES; //设置为NO后，不再响应touch方法
        self.multipleTouchEnabled = YES;
        //控制子视图不能超出父视图的范围
        self.clipsToBounds = YES;
        //添加子视图
        [self subView];
    }
    return self;
}

//子视图懒加载
-(void)subView{
    //选择器背景
    UIView *orderConfirmPickerBackView=[[UIView alloc]initWithFrame:CGRectMake(0,0, [ShiPei CGFloatMakeScale:840], [ShiPei CGFloatMakeScale:250])];
    [orderConfirmPickerBackView setBackgroundColor:[UIColor whiteColor]];
    [orderConfirmPickerBackView setUserInteractionEnabled:YES];
    
    //选择器标题与取消确定按钮
    UIButton *cancelPickerButton=[[UIButton alloc]initWithFrame:[ShiPei CGRectMakeScale:0 :10 :100 :30]];
    [cancelPickerButton addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    [cancelPickerButton setTitleColor:FUIColorFromRGB(0x666666) forState:UIControlStateNormal];
    [cancelPickerButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelPickerButton.titleLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeScale:16]]];
    self.cancelPickerButton=cancelPickerButton;
    
    UIButton *quedingPickerButton=[[UIButton alloc]initWithFrame:[ShiPei CGRectMakeScale:740 :10 :100 :30]];
    [quedingPickerButton addTarget:self action:@selector(queren) forControlEvents:UIControlEventTouchUpInside];
    [quedingPickerButton setTitleColor:FUIColorFromRGB(0x1b75cc) forState:UIControlStateNormal];
    [quedingPickerButton setTitle:@"确定" forState:UIControlStateNormal];
    [quedingPickerButton.titleLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeScale:16]]];
    self.quedingPickerButton=quedingPickerButton;
    
    UILabel *titlePicker=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:120 :10 :600 :30]];
    [titlePicker setText:@"选择时间"];
    [titlePicker setUserInteractionEnabled:YES];
    [titlePicker setBackgroundColor:[UIColor clearColor]];
    [titlePicker setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeScale:16]]];
    [titlePicker setTextAlignment:NSTextAlignmentCenter];
    [titlePicker setTextColor:FUIColorFromRGB(0x666666)];
    [titlePicker setLineBreakMode:NSLineBreakByTruncatingTail];
    
    UIDatePicker *datePicker=[[UIDatePicker alloc] initWithFrame:[ShiPei CGRectMakeScale:0 :40 :840 :200]];
    [datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged ];
    datePicker.datePickerMode = UIDatePickerModeDate;
    self.datePicker=datePicker;
    
    UIImageView *pickerline01=[[UIImageView alloc]initWithFrame:[ShiPei CGRectMakeScale:0 :0 :840 :1]];
    [pickerline01 setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.17]];
    UIImageView *pickerline02=[[UIImageView alloc]initWithFrame:[ShiPei CGRectMakeScale:0 :50 :840 :1]];
    [pickerline02 setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.17]];
    
    [orderConfirmPickerBackView addSubview:cancelPickerButton];
    [orderConfirmPickerBackView addSubview:quedingPickerButton];
    [orderConfirmPickerBackView addSubview:titlePicker];
    [orderConfirmPickerBackView addSubview:datePicker];
    [orderConfirmPickerBackView addSubview:pickerline01];
    [orderConfirmPickerBackView addSubview:pickerline02];
    [self addSubview:orderConfirmPickerBackView];
}

-(void)dateChanged:(id)sender{
    UIDatePicker *control = (UIDatePicker*)sender;
    _date = control.date;
}

-(void)cancel{
    if([_delegate respondsToSelector:@selector(onCancel:)]){
        [_delegate onCancel:self];
    }
}

-(void)queren{
    if([_delegate respondsToSelector:@selector(onQueren:)]){
        [_delegate onQueren:self];
    }
}




@end